package com.fzu.mmdbproxyframework.context;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.fzu.mmdbproxyframework.configurationmanager.MMDBConfigRead;
import com.fzu.mmdbproxyframework.configurationmanager.SourceDBConfigRead;
import com.fzu.mmdbproxyframework.model.Rule;

public class ConfigContext {
	public  List<Rule> ruleList = new LinkedList<Rule>();
	public int MMDBNumbers=0;
	public  String AllTables;
	public  String Master_Slave;
	public 	String MMDBType;
	public  String SourceDBNodeType;
	public  int updatePeriod=0;
	private String createTableStr;
	private long keepaliveTime=0;
	private String insertStr;
	private String selectStr;
	private String updateStr;
	private String dropTableIfExitStr;
	private Map<String,String> attributeMap;
	
	
//	public static ConfigContext getInstance(){
//		return configContext;
//	}
	public  List<Rule> getRuleList() {
		return ruleList;
	}

	public  void setRuleList(List<Rule> ruleList) {
		this.ruleList = ruleList;
	}
	
	public  String getMaster_Slave() {
		if(Master_Slave==null){
			for(Rule rule :ruleList){
				if(rule.getProperty().get("name").equalsIgnoreCase("Server")){
					setMaster_Slave(rule.getProperty().get("Master_Slave"));
				}
			}
		}
		
		return Master_Slave;
	}

	public  String getSourceDBNodeType() {
		if(SourceDBNodeType==null){
			for(Rule rule :ruleList){
				if(rule.getProperty().get("name").equalsIgnoreCase("Server")){
					setSourceDBNodeType(rule.getProperty().get("SourceDBNodeType"));
					break;
				}
			}
		}
		return SourceDBNodeType;
	}

	public  int getUpdatePeriod() {
		if(updatePeriod==0){
			for(Rule rule :ruleList){
				if(rule.getProperty().get("name").equalsIgnoreCase("Server")){
					setUpdatePeriod(Integer.parseInt(rule.getProperty().get("updatePeriod")));
				}
			}
		}
		return updatePeriod;
	}

	public void setMaster_Slave(String master_Slave) {
		Master_Slave = master_Slave;
	}

	public void setSourceDBNodeType(String sourceDBNodeType) {
		SourceDBNodeType = sourceDBNodeType;
	}

	public void setUpdatePeriod(int updatePeriod) {
		this.updatePeriod = updatePeriod;
	}

	public int getMMDBNumbers() {
		if(MMDBNumbers==0){
			for(Rule rule :ruleList){
				if(rule.getProperty().get("name").equalsIgnoreCase("Server")){
					setMMDBNumbers(Integer.parseInt(rule.getProperty().get("MMDBNumbers")));
					break;
				}
			}
		}
		return MMDBNumbers;
	}

	public void setMMDBNumbers(int mMDBNumbers) {
		MMDBNumbers = mMDBNumbers;
	}

	public String getAllTables() {
		if(AllTables==null){
			System.out.println("AllTables==null");
			for(Rule rule :ruleList){
				System.out.println("rule.Name()="+rule.getProperty().get("name"));
				if(rule.getProperty().get("name").equalsIgnoreCase("Server")){
					setAllTables(rule.getProperty().get("AllTables"));
					break;
				}
			}
		}
		System.out.println("AllTables="+AllTables);
		return AllTables;
	}

	public void setAllTables(String allTables) {
		AllTables = allTables;
	}

	public String getCreateTableStr() {
		if(createTableStr ==null){
			for(Rule rule :this.getRuleList()){
				if(rule.getProperty().get("name").equalsIgnoreCase("heartbeat")){
					setCreateTableStr(rule.getProperty().get("createTableStr"));
					break;
				}
			}
		}
		return createTableStr;
	}

	public void setCreateTableStr(String createTableStr) {
		this.createTableStr = createTableStr;
	}

	public long getKeepaliveTime() {
		if(keepaliveTime == 0){
			for(Rule rule :this.getRuleList()){
				if(rule.getProperty().get("name").equalsIgnoreCase("heartbeat")){
					setKeepaliveTime(Long.parseLong(rule.getProperty().get("keepaliveTime")));
					break;
				}
			}
		}
		return keepaliveTime;
	}

	public void setKeepaliveTime(long keepaliveTime) {
		this.keepaliveTime = keepaliveTime;
	}

	public String getInsertStr() {
		if(insertStr == null){
			for(Rule rule : this.getRuleList()){
				if(rule.getProperty().get("name").equalsIgnoreCase("heartbeat")){
					setInsertStr(rule.getProperty().get("insertStr"));
					break;
				}
			}
		}
		return insertStr;
	}

	public void setInsertStr(String insertStr) {
		this.insertStr = insertStr;
	}

	public String getUpdateStr() {
		if(updateStr == null){
			for(Rule rule : this.getRuleList()){
				if(rule.getProperty().get("name").equalsIgnoreCase("heartbeat")){
					setUpdateStr(rule.getProperty().get("updateStr"));
					break;
				}
			}
		}
		return updateStr;
	}

	public void setUpdateStr(String updateStr) {
		this.updateStr = updateStr;
	}

	public Map<String, String> getAttributeMap() {
		return attributeMap;
	}

	public void setAttributeMap(Map<String, String> attributeMap) {
		this.attributeMap = attributeMap;
	}

	public String getDropTableIfExitStr() {
		if(dropTableIfExitStr == null){
			for(Rule rule : this.getRuleList()){
				if(rule.getProperty().get("name").equalsIgnoreCase("heartbeat")){
					setDropTableIfExitStr(rule.getProperty().get("dropTableIfExitStr"));
					break;
				}
			}
		}
		return dropTableIfExitStr;
	}

	public void setDropTableIfExitStr(String dropTableIfExitStr) {
		this.dropTableIfExitStr = dropTableIfExitStr;
	}

	public String getSelectStr() {
		if(selectStr == null){
			for(Rule rule : this.getRuleList()){
				if(rule.getProperty().get("name").equalsIgnoreCase("heartbeat")){
					setSelectStr(rule.getProperty().get("selectStr"));
					break;
				}
			}
		}
		return selectStr;
	}

	public void setSelectStr(String selectStr) {
		this.selectStr = selectStr;
	}
	public String getMMDBType() {
		if(MMDBType==null){
			for(Rule rule :ruleList){
				if(rule.getProperty().get("name").equalsIgnoreCase("Server")){
					setMMDBType(rule.getProperty().get("MMDBType"));
					break;
				}
			}
		}
		return MMDBType;
	}
	public void setMMDBType(String mMDBType) {
		MMDBType = mMDBType;
	}
	
	
}
